<< define "menu/index.html" >>
<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>资源管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8"/>
    <link rel="stylesheet" href="/static/css/font.css">
    <link rel="stylesheet" href="/static/css/xadmin.css">
    <link rel="stylesheet" href="/static/css/mbgadmin.css">
    <script src="/static/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/js/xadmin.js"></script>
    <script type="text/javascript" src="/static/js/mbgadmin.js"></script>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card" style="padding: 10px">
                <div class="layui-card-body">
                    <table id="menu" class="layui-hide" lay-filter="menu"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="tools">
    <div class="layui-btn-container">
        <a class="layui-btn layui-btn-primary layui-btn-xs layui-bg-blue" lay-event="add">新增子菜单</a>
        <a class="layui-btn layui-btn-primary  layui-btn-xs layui-bg-red" lay-event="del">删除子菜单</a>
    </div>
</script>
<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加一级菜单</button>
    </div>
</script>
<script>
    layui.use(["treeTable","layer",'dropdown'],function (){
        let treeTable = layui.treeTable;
        let layer = layui.layer;
        var inst = treeTable.render({
            elem: '#menu',
            url: '/menus/lists', // 此处为静态模拟数据，实际使用时需换成真实接口
            tree: {
                view:{
                    expandAllDefault:true,
                },
                /*
                // 异步加载子节点
                async: {
                  enable: true,
                  url: '/static/2.8/json/treeTable/demo-async.json', // 此处为静态模拟数据，实际使用时需换成真实接口
                  autoParam: ["parentId=id"]
                }
                */
            },
            maxHeight: '800px',
            toolbar: '#toolbar',
            editTrigger:'dblclick',

            cols: [[
                {type: 'checkbox', fixed: 'left'},
                {field: 'ID', title: 'ID', },
                {field: 'name', title: '菜单名称',edit:'text',width:400},
                {field: 'URL', title: 'URL', edit:'text'},
                {field: 'request_type', title: '请求方式', edit:'text'},
                {field: 'icons', title: '图标',edit:'text'}, //图标在这里有个坑 不能用icon
                {field: 'parentID', title: '父ID'},
                {field: 'parentName', title: '父菜单名称'},
                {field: 'level', title: '层级'},
                {field: 'type', title: '类型',templet(d){
                        if (d.type===0) {
                            return "菜单"
                        }else if (d.type===1){
                            return "按钮"
                        }else if (d.type===2){
                            return "资源"
                        }
                    }},
                {field: 'sort', title: '排序',edit:'textarea'},
                { fixed: "right", title: "操作", align: "center", toolbar: "#tools", width:206}
            ]],
            // page: { // 支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档//curr: 5, //设定初始在第 5 页
            //     // limit:1
            // }
        });
        treeTable.on('tool(menu)',function (obj){
            let layEvent = obj.event;
            let trdata = obj.data;
            console.log(trdata)
                if (layEvent ==="add"){
                layer.open({
                    id:'menu_add',
                    type:2,
                    title:"添加子菜单",
                    skin: 'layui-layer-molv',
                    closeBtn:2,
                    anim:5,
                    area: ['680px', '520px'],
                    maxmin: false, //开启最大化最小化按钮
                    content:"/menus/createMenus",
                    success:function (layero,index){
                        // 获取子页面的iframe
                        var iframe = window['layui-layer-iframe'+index];
                        // 向子页面的全局函数child传参
                        iframe.child(trdata);
                    },
                })
            }
            if (layEvent === 'del'){
                layer.confirm("确定删除吗？",{icon:3},function (index){
                    layer.close(index)
                    let loadIndex = layer.load(2)
                    $.ajax({
                        url:"/menus/del/"+trdata.ID,
                        type:'GET',
                        success:function (res){
                            if (res.code===0){
                                layer.close(loadIndex)
                                layer.msg(res.msg)
                                obj.del();
                            }else {
                                layer.msg(res.msg)
                            }
                        }
                    })
                })
            }
        })
        treeTable.on('toolbar(menu)',function (obj){
           var trdata = {
               'ID':"",
               "name":"",
               "level":-1
           }
            switch (obj.event){
                case 'add':
                    layer.open({
                        id:'menu_add',
                        type:2,
                        title:"添加一级菜单",
                        skin: 'layui-layer-molv',
                        closeBtn:2,
                        anim:5,
                        area: ['680px', '520px'],
                        maxmin: false, //开启最大化最小化按钮
                        content:"/menus/createMenus",
                        success:function (layero,index){
                            // 获取子页面的iframe
                            var iframe = window['layui-layer-iframe'+index];
                            // 向子页面的全局函数child传参
                            iframe.child(trdata);
                        },
                    })
            }
        })
        treeTable.on('edit(menu)', function (obj){
            var field = obj.field; // 得到修改的字段
            var value = obj.value // 得到修改后的值
            var data = obj.data // 得到所在行所有键值
            if (field ==="icons"){
                field = "icon"
            }
            let loadIndex = layer.load(2);
           $.post("/menus/update/"+data.ID,{"field":field,"value":value},function (res){
                if (res.code ===0){
                    layer.close(loadIndex)
                    layer.msg(res.msg)
                }else {
                    layer.close(loadIndex)
                    layer.msg(res.msg)
                treeTable.reloadData('menu',{
                    where:{},
                    scrollPos:true
                })
                }
           })

        });
    })

</script>
</body>
</html>
<< end >>